package com.ksyun.whcg.zhanglulu;
import java.lang.reflect.Field;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * @Author: ashtar555
 * @Description: TODO 描述内容
 * @DateTime: 2023/4/21 13:53
 **/
public class CollectionPrinter {
    public static void print(List<?> collection) {
        // 输出表头
        Field[] fields = collection.get(0).getClass().getDeclaredFields();
        for (Field field : fields) {
            if (field.isAnnotationPresent(com.ksyun.whCollectionPrintercg.zhanglulu.FieldName.class)) {
                com.ksyun.whCollectionPrintercg.zhanglulu.FieldName annotation = field.getAnnotation(com.ksyun.whCollectionPrintercg.zhanglulu.FieldName.class);
                System.out.print(annotation.value() + "\t");
            }
        }
        System.out.println();

        // 输出数据
        for (Object item : collection) {
            for (Field field : fields) {
                if (field.isAnnotationPresent(com.ksyun.whCollectionPrintercg.zhanglulu.FieldName.class)) {
                    try {
                        Object value = field.get(item);
                        if (value instanceof LocalDate) {
                            value = ((LocalDate) value).format(DateTimeFormatter.ISO_LOCAL_DATE);
                        }
                        System.out.print(value + "\t");
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    }
                }
            }
            System.out.println();
        }
    }
}
